<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class GregisterForm extends CFormModel
{
	public $username;
	public $password;
    /**
     * @var $_identity UserIdentity
     */
	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password', 'required'),
            array('username', 'length', 'max' => 60,'min'=>5),
            array('password', 'length', 'max' => 250,'min'=>6),
		);
	}

	/**
	 * Declares attribute labels.
     * @return array
     */
	public function attributeLabels()
	{
		return array(
			'username'=>'Tài khoản',
			'password'=>'Mật khẩu',
		);
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			Yii::app()->user->login($this->_identity);
			return true;
		}
		else
			return false;
	}
}
